//
//  SRCommonViewController.m
//  EnglishTalk
//
//  Created by yanming.huym on 15/5/20.
//  Copyright (c) 2015年 ishowtalk. All rights reserved.
//

#import "SRCommonViewController.h"

#import <UMMobClick/MobClick.h>

@interface SRCommonViewController ()

@property (strong, nonatomic) MBProgressHUD *hud;

@end

@implementation SRCommonViewController

-(id)initControllerWithParameters:(NSDictionary*)parameters
{
    self = [self initWithNibName:nil    bundle:nil];
    if (self) {
  
    }
    return self;
}


-(instancetype)initControllerWithRemoteNotificationParameters:(NSDictionary *)parameters{
    self = [self initWithNibName:nil    bundle:nil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [FZStyleSheet currentStyleSheet].color_2;
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    if (self.navigationController.viewControllers.count > 0) {
        UIBarButtonItem * backItem=[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
        [self.navigationItem setBackBarButtonItem:backItem];
    }
    
    self.loadingView = [[FZLoadingView alloc] initWithFrame:self.view.bounds containerView:self.view];
    
//    NSLog(@"viewDidLoad: %@",[self class]);
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:NSStringFromClass([self class])];
//    [((AppDelegate *)[UIApplication sharedApplication].delegate) initNavigation];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [MobClick endLogPageView:NSStringFromClass([self class])];
}

- (void)setNavgationbarBackWhite {
        //白色
    UIImage *originBackImage = [UIImage imageNamed:@"all_icon_back_white"];
    [self.navigationController.navigationBar setBackIndicatorImage:originBackImage];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
}

- (void)setNavgationbarBackGray {
        //灰色
    UIImage *originBackImage = [UIImage imageNamed:@"all_icon_back_gray"];
    [self.navigationController.navigationBar setBackIndicatorImage:originBackImage];
    [self.navigationController.navigationBar setTintColor:[UIColor colorWithHex:0x7c7c7c]];
}

-(void)doBack {
    [self stopProgressHUD];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

#pragma mark - Public Method

- (void)setRightBarButtonItem:(UIBarButtonItem *)rightBarItem {
//    UIBarButtonItem *spaceFixBarItem = [[UIBarButtonItem alloc] init];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                   target:nil action:nil];
    negativeSpacer.width = -5;//这个数值可以根据情况自由变化
    self.navigationItem.rightBarButtonItems = @[negativeSpacer, rightBarItem];
}

- (void)setLeftBarButtonItem:(UIBarButtonItem *)leftBarItem {
    //    UIBarButtonItem *spaceFixBarItem = [[UIBarButtonItem alloc] init];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                   target:nil action:nil];
    negativeSpacer.width = -20;//这个数值可以根据情况自由变化
    self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftBarItem];
}

- (void)setClearNavigationBar:(BOOL)isClear {
    if (isClear) {
        self.navigationController.navigationBarHidden = NO;
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                      forBarMetrics:UIBarMetricsDefault];
//        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
        self.navigationController.navigationBar.clipsToBounds = YES;
    } else {
        //复原
        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[FZStyleSheet currentStyleSheet].colorOfSeperatorOnLightBackground]];
        self.navigationController.navigationBar.clipsToBounds = NO;
    }
   
}

- (void)setLeftBarButtonItemWithWhite
{
    UIImage *image = [UIImage imageNamed:@"common_back_white"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [UINavigationBar appearance].backIndicatorImage = image;
    [UINavigationBar appearance].backIndicatorTransitionMaskImage = image;
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage  imageNamed:@"common_back_white"] forState:UIControlStateNormal];
    [leftButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    leftButton.frame = CGRectMake(0, 0, 22, 22);
    [leftButton addTarget: self action: @selector(doBack) forControlEvents: UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

- (void)setLeftBarButtonItemWithVideo {
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [backButton setBackgroundImage:[UIImage imageNamed:@"dubbing_icon_back_graybg"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(doBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                   target:nil action:nil];
    negativeSpacer.width = -5;//这个数值可以根据情况自由变化
    self.navigationItem.leftBarButtonItems = @[negativeSpacer, item];

    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

- (void)setLeftBarButtonItemWithVideoPopGestureEnable:(BOOL)enable {
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [backButton setBackgroundImage:[UIImage imageNamed:@"dubbing_icon_back_graybg"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(doBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                   target:nil action:nil];
    negativeSpacer.width = -5;//这个数值可以根据情况自由变化
    self.navigationItem.leftBarButtonItems = @[negativeSpacer, item];
    
    if (enable) {
        self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    }
}

- (void)setLeftBarButtonItemWithGray
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage  imageNamed:@"common_back_gray"] forState:UIControlStateNormal];
    [leftButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, -7, 0, 0);
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    [leftButton addTarget: self action: @selector(doBack) forControlEvents: UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                   target:nil action:nil];
    negativeSpacer.width = -1;//这个数值可以根据情况自由变化
    self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftBarItem];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}


- (void)setLeftBarButtonItemWithGrayPopGestureEnable:(BOOL)enable {
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage  imageNamed:@"common_back_gray"] forState:UIControlStateNormal];
    [leftButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, -7, 0, 0);
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    [leftButton addTarget: self action: @selector(doBack) forControlEvents: UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                   target:nil action:nil];
    negativeSpacer.width = -1;//这个数值可以根据情况自由变化
    self.navigationItem.backBarButtonItem = leftBarItem;
}

- (void)setNavgationbarTitleColorToWhite
{
//    NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
//                                               [UIColor whiteColor],NSForegroundColorAttributeName,
//                                               [UIFont systemFontOfSize:15],
//                                               NSFontAttributeName, nil];
//    [self.navigationController.navigationBar setTitleTextAttributes:navbarTitleTextAttributes];
    
}

- (void)setNavgationbarTitleColorToDefault
{
//    self.navigationController.navigationBar.titleTextAttributes = nil;
}


#pragma mark - Loading & tip

- (void)updateHUDgrogress:(MBProgressHUDMode)mode  labelText:(NSString*)text
{
    dispatch_async(dispatch_get_main_queue(), ^{

    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.view addSubview:self.hud];
    self.hud.mode = mode;
    self.hud.labelText = text;
        
    });
    
}



-(void) updateMyProgressTask:(CGFloat)pregress{
    dispatch_async(dispatch_get_main_queue(), ^{

    self.hud.progress = pregress;
    });
}

-(void)showMBProGressHUD
{
    dispatch_async(dispatch_get_main_queue(), ^{

    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.hud];
    self.hud.mode = MBProgressHUDModeDeterminate;
//    self.hud.delegate = self;
    [self.hud show:YES];
    });

}


- (void)startProgressHUD {
 
    
    WEAKSELF
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            //    HUD.delegate = self;
        weakSelf.hud = HUD;
        
        weakSelf.hud.labelText = @"正在加载...";
        [weakSelf.view bringSubviewToFront:HUD];
        [weakSelf.hud show:YES];
    });
}

- (void)startProgressHUDWithView:(UIView*)view{

    WEAKSELF
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
            //    HUD.delegate = self;
        weakSelf.hud = HUD;
        
        weakSelf.hud.labelText = @"正在加载...";
        [weakSelf.view bringSubviewToFront:HUD];
        [weakSelf.hud show:YES];
    });
}

- (void)stopProgressHUD {
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:NO];
        if (self.hud) {
            [self.hud hide:NO];
        }
    });
}

- (void)startProgressHUDWithText:(NSString *)text{

    WEAKSELF
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            //    HUD.delegate = self;
        weakSelf.hud = HUD;
        
        weakSelf.hud.labelText = text;
        [weakSelf.hud show:YES];
    });
}

- (void)showHUDError {
    [self showHUDHintWithText:@"加载失败"];
}
- (void)showHUDErrorMessage:(NSString *)message{
    [self showHUDHintWithText:message];
}

- (void)showHUDHintWithText:(NSString *)text {
    dispatch_async(dispatch_get_main_queue(), ^{

    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hub.mode = MBProgressHUDModeText;
    //    hub.labelText = text;
    hub.detailsLabelText = text;
    hub.removeFromSuperViewOnHide = YES;
    
        [hub show:YES];
        [hub hide:YES afterDelay:2];
    });
   
}

- (void)showHUDHintWithText:(NSString *)text image:(UIImage *)image {
    dispatch_async(dispatch_get_main_queue(), ^{
        
        MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hub.mode = MBProgressHUDModeCustomView;
            //    hub.labelText = text;
        hub.detailsLabelText = text;
        hub.removeFromSuperViewOnHide = YES;
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(0, 0, 56, 56);
        hub.customView = imageView;
        
        [hub show:YES];
        [hub hide:YES afterDelay:2];
    });
    
}

#pragma mark - FZLoadingView

- (void)showLoadingViewWithCompletedBlock:(LVAnimationCompletedBlock)completedBlock {
    [self.loadingView showAnimated:YES completionHandler:completedBlock];
}

- (void)failedLoadingViewWithTitle:(NSString *)title {
    if (title) {
        [self.loadingView failedWithTitle:title];
    } else {
        [self.loadingView failed];
    }
}
- (void)emptyLoadingViewWithTitle:(NSString *)title {
    if (title) {
        [self.loadingView emptyWithTitle:title subTitle:nil];
    } else {
        [self.loadingView empty];
    }
}
- (void)hideLoadingView {
    [self.loadingView hide];
}


#pragma mark - Orientation

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return  UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationMaskPortrait);//系统默认不支持旋转功能
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return NO;
}

+ (BOOL)errorCode:(NSInteger)codeStauts
{
    if ((codeStauts == 401)||(codeStauts == 402)||(codeStauts == 403)) {
//        [SRLoginUser setIsSignIn:NO];
        return YES;
    }
    return NO;
}

- (void)showTabBar
{
    if (self.tabBarController.tabBar.hidden == NO)
    {
        return;
    }
    UIView *contentView;
    if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]]) {
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    } else {
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    }
    contentView.frame = CGRectMake(0, 0,  contentView.bounds.size.width, contentView.bounds.size.height);
    
    self.tabBarController.tabBar.hidden = NO;
}
// 隐藏tabbar
- (void)hideTabBar {
    if (self.tabBarController.tabBar.hidden == YES)
    {
        return;
    }
    UIView *contentView;
    if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]]) {
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    } else {
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    }
    contentView.frame = CGRectMake(0,  0,  contentView.bounds.size.width, contentView.bounds.size.height );
    self.tabBarController.tabBar.hidden = YES;
}

@end
